/**
 * 
 */
package GUI.network;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import officeserver.office.request.AdminRequest;
import officeserver.office.request.AdminRequest.TYPE;
import officeserver.office.request.OfficeRequest;

/**
 * @author Chris Bayruns
 * 
 */
public class NetworkConnection {
    private String host;
    private int port;
    private Socket mySocket;
    private ObjectOutputStream outStream;
    private ObjectInputStream inStream;
    private DataInputStream din;
    public final int timeout = 20; // seconds

    /**
     * @author Chris Bayruns
     * @param host
     *            The host to connect to.
     * @param port
     *            The port to connect to.
     * @throws IOException
     * @throws UnknownHostException
     */
    public NetworkConnection(String host, int port)
	    throws UnknownHostException, IOException {
	this.host = host;
	this.port = port;
	connect();
    }

    public void connect() throws UnknownHostException, IOException {
	mySocket = new Socket(host, port);
	outStream = new ObjectOutputStream(mySocket.getOutputStream());
	// outStream.flush();
	inStream = new ObjectInputStream(mySocket.getInputStream());
	//din = new DataInputStream(mySocket.getInputStream());
    }

    public void disconnect() throws IOException {
	if (mySocket != null) {

	    outStream.writeObject(new AdminRequest(TYPE.CON_DISCONNECT));
	    outStream.flush();
	    outStream.flush();
	    //outStream.reset();
	    outStream.close();
	    //inStream.reset();
	    inStream.close();
	    mySocket.close();
	    // mySocket.
	}
    }

    /**
     * @author Chris Bayruns
     * @return If the net is connected.
     */
    public boolean isConnected() {
	if (mySocket != null) {
	    return mySocket.isConnected();
	} else {
	    return false;
	}
    }

    /**
     * @return the host
     */
    public String getHost() {
	return host;
    }

    /**
     * @param host
     *            the host to set
     */
    public void setHost(String host) {
	this.host = host;
    }

    /**
     * @return the port
     */
    public int getPort() {
	return port;
    }

    /**
     * @param port
     *            the port to set
     */
    public void setPort(int port) {
	this.port = port;
    }

    public OfficeRequest request(OfficeRequest in) throws IOException,
	    ClassNotFoundException {
	OfficeRequest result = null;
	long time;

	if (mySocket.isConnected()) {
	    outStream.writeObject(in);
	    outStream.flush();
	    time = System.currentTimeMillis();

	    Object temp = inStream.readObject();
		result = (OfficeRequest)temp;
	}

	return result;
    }
    
    private OfficeRequest bytesToObject( byte bytes[] ) throws IOException, ClassNotFoundException
    {
    	OfficeRequest result = null;
    	
		result = (OfficeRequest) new java.io.ObjectInputStream(new
		java.io.ByteArrayInputStream(bytes)).readObject();

		return result;
    }
    
    private static byte[] toBytes(Object object)
    {
		java.io.ByteArrayOutputStream baos = new
		java.io.ByteArrayOutputStream();
		try{
			java.io.ObjectOutputStream oos = new
			java.io.ObjectOutputStream(baos);
			oos.writeObject(object);
		}catch(java.io.IOException ioe){
			ioe.printStackTrace();
		}
		
		return baos.toByteArray();
	} 
    
}
